home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / x11 / lib / draws.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-09  |  8.4 KB  |  293 lines

  1. /* ============= DrawShape . C ==========================
  2. %        Copyright (C)    Jin Guojun        %
  3. %    Draw a square in a given image window.        %
  4. %    A Bug in X Window is that in DrawRectangle,    %
  5. %    real width and height are equal to given width    %
  6. %    and height "+ 1".  So if you want to draw a    %
  7. %    10 x 10 rectangle, Draw a 9 x 9 one instead.    %
  8. % AUTHOR:    Jin Guojun - LBL  ===== 4/1/91 ========*/
  9.  
  10. #include "function.h"
  11.  
  12. #ifndef    MIN_SUBWINSIDE
  13. #define    MIN_SUBWINSIDE    4
  14. #endif
  15.  
  16. void    /* who = 0 for draws, 1 for texts, and -1 for both    */
  17. superimpose_images(img, who, x0, y0, cw, ch)
  18. U_IMAGE    *img;
  19. {
  20. XRectangle    r;    /*    Range    */
  21. char    font_sname[16];    /* font name string    */
  22. int    sub_img = img->sub_img, x = img->sub_img_x, y = img->sub_img_y,
  23.     h = img->sub_img_h, w = img->sub_img_w,
  24.     font_w = img->font_w, font_h = img->font_h;
  25. register int    i = img->draws;
  26. /* r.x0 = x0;    r.y0 = y0;    r.cs = cw;    r.ls = ch;    */
  27. r.x = x0;    r.y = y0;    r.width = cw;    r.height = ch;
  28.  
  29. if (img->superimpose && (i | img->texts))    {
  30. register superimpose_elems*    si_ep = img->superimpose[0];
  31.     XSetClipRectangles(img->dpy, img->gc, 0, 0, &r, 1, No);
  32.     if (i && who<1) while (i--)    {
  33.     XSetForeground(img->dpy, img->gc, si_ep[i].color);
  34.     XSetLineAttributes(img->dpy, img->gc, si_ep[i].elem.draw.line_w,
  35.         LineSolid, CapButt, JoinMiter);
  36.     img->sub_img = si_ep[i].e_type;
  37.     img->sub_img_x = si_ep[i].x0;
  38.     img->sub_img_y = si_ep[i].y0;
  39.     img->sub_img_w = si_ep[i].w;
  40.     img->sub_img_h = si_ep[i].h;
  41.     Draws(img, 0, No, img->sub_img);
  42.     }
  43.     XSetLineAttributes(img->dpy, img->gc, 0, 0, CapButt, 0);
  44.     if (who && (i=img->texts))    {
  45.     si_ep = img->superimpose[1];
  46.     while (i--)    {
  47.         SetFontGetSize(img, sprintf(font_sname, "%dx%d",
  48.             si_ep[i].elem.text.fw, si_ep[i].elem.text.fh));
  49.         XSetForeground(img->dpy, img->gc, si_ep[i].color);
  50.         img->sub_img = si_ep[i].e_type;
  51.         img->sub_img_x = si_ep[i].x0;
  52.         img->sub_img_y = si_ep[i].y0;
  53.         img->sub_img_w = si_ep[i].w;
  54.         img->sub_img_h = si_ep[i].h;
  55.         Draws(img, si_ep[i].elem.text.content, No, -1);
  56.     }
  57.     }
  58.     XSetClipMask(img->dpy, img->gc, None);
  59. }
  60. img->sub_img = sub_img;
  61. img->sub_img_x = x;    img->sub_img_y = y;
  62. img->sub_img_w = w;    img->sub_img_h = h;
  63. SetFontGetSize(img, sprintf(font_sname, "%dx%d", font_w, font_h));
  64. }
  65.  
  66. superimpose_handle(img, n, x, y)
  67. register Image*    img;
  68. register int    n;
  69. {
  70. if (img->superimpose)    {
  71. int    m=0, mx, my;
  72. register superimpose_elems* si_ep = img->superimpose[n<0];
  73.  
  74. if (img->sub_img)    Draws(img, 0, 1, img->sub_img);
  75. if (n < 0)    {
  76. char    font_sname[16];    /* font name string    */
  77.     n = ~n;    /* text    */
  78.     SetFontGetSize(img, sprintf(font_sname, "%dx%d",
  79.         si_ep[n].elem.text.fw, si_ep[n].elem.text.fh));
  80. } else    n--,    /* draws    */
  81. XSetLineAttributes(img->dpy, img->gc, my=si_ep[n].elem.draw.line_w, 0, 0, 0);
  82. XSetForeground(img->dpy, img->gc, si_ep[n].color);
  83. img->sub_img_x = si_ep[n].x0;
  84. img->sub_img_y = si_ep[n].y0;
  85. img->sub_img_w = si_ep[n].w;
  86. img->sub_img_h = si_ep[n].h;
  87. img->sub_img = si_ep[n].e_type;
  88. while (!ImageEvent(img, ButtonAction));/* X Events responding time is 200 ms */
  89. XBell(img->dpy, 50);
  90.  
  91. do {    /* move object    */
  92. XButtonEvent*    xb = img->event;
  93.     if (ImageEvent(img, PointerMotionMask))    {    /* object moved */
  94.         Draws(img, img->sub_img<0?si_ep[n].elem.text.content:0, 1,
  95.             img->sub_img);
  96.         img->sub_img_x = si_ep[n].x0 + xb->x - x;
  97.         img->sub_img_y = si_ep[n].y0 + xb->y - y;
  98.         Draws(img, img->sub_img<0?si_ep[n].elem.text.content:0, 1,
  99.             img->sub_img);
  100.         img->update = m = True;
  101.     }
  102. } while (!ImageEvent(img, ButtonAction));
  103. RemoveImageEvent(img, ButtonAction);
  104. if (m)    {    /* updated    */
  105. register int    w = img->sub_img_x,    h = img->sub_img_y;
  106.     m = my;
  107.     x = MIN(si_ep[n].x0, w) - m;
  108.     y = MIN(si_ep[n].y0, h) - m;
  109.     mx = MAX(si_ep[n].x0, w);
  110.     my = MAX(si_ep[n].y0, h);
  111.     si_ep[n].x0 = w,    si_ep[n].y0 = h;
  112.     w = img->sub_img_w;    h = img->sub_img_h;
  113.     if (n = img->sub_img==DrawsArc)
  114.         x -= w,    y -= h;
  115.     else if (img->sub_img < 0)    /* text    */
  116.         x -= img->font_w,    y -= img->font_h;
  117.     w = mx + (w+m << n);    h = my + (h+m << n);
  118.         if (img->refresh_pixmap)
  119.         XCopyArea(img->dpy, img->refresh_pixmap, img->win, img->gc,
  120.             x, y, w, h, x, y);
  121.         else    XPutImage(img->dpy, img->win, img->gc, img->image,
  122.                         x, y, x, y, w, h);
  123.     superimpose_images(img, -1, x, y, w, h);
  124. }
  125. img->sub_img = 0;
  126. }
  127. }
  128.  
  129.  
  130. /*
  131. %    Draws Text string, Arc, Line, and Rectangle.
  132. %    img->sub_img_? must be set before this routine called.
  133. */
  134. Draws(img, x, revs, tp)
  135. register Image    *img;
  136. register int    x;
  137. {
  138. int    y, w=img->sub_img_w, h=img->sub_img_h, stp=x;
  139. register float    f = img->mag_fact;
  140. if (f<0)    f = -1/f;
  141. if (x<1)    x = MIN_SUBWINSIDE;
  142.  
  143. if (tp>0 && tp!=DrawsLine && ((w*=f)<x || (h*=f)<x))
  144.     return    False;
  145.  
  146. if (revs)
  147.     XSetFunction(img->dpy, img->gc, revs>1 ? revs : GXinvert);
  148. x = (img->sub_img_x - img->mag_x) * f;
  149. y = (img->sub_img_y - img->mag_y) * f;
  150. #ifndef    SCROLLBAR_on_CANVAS
  151. x -= img->x0;    y -= img->y0;
  152. #endif
  153.  
  154. #define    XDX    img->dpy,img->win,img->gc
  155.  
  156. if (tp < 0)    {
  157.     w = strlen(stp);    h = img->font_h;
  158.     do {
  159.     register int    i = strchr(stp, CR) - stp;
  160.         w -= abs(i);
  161.         if (i<0)    i = strlen(stp) - 1;
  162.         XDrawString(XDX, x, y, stp, i);
  163.         y += h;    stp += ++i;
  164.     } while (w>0);
  165. } else if (tp==DrawsLine)    XDrawLine(XDX, x, y, x+w, y+h);
  166. else if (tp==DrawsArc)
  167.     XDrawArc(XDX, x-w, y-h, w<<1, h<<1, 0, 360<<6);
  168. else    XDrawRectangle(XDX, x, y, --w, --h);
  169. XSetFunction(img->dpy, img->gc, GXcopy);
  170. return    tp;
  171. }
  172.  
  173.     /*=======================================
  174.     %    Draw a vertical line (mark)    %
  175.     %    in a given image window        %
  176.     =======================================*/
  177. DrawVMark(img, x, clean)
  178. Image    *img;
  179. {
  180. if (x == img->mark_x)    return    0;
  181.  
  182. XSetFunction(img->dpy, img->gc, GXinvert /* GXxor */);
  183. if (clean)    {
  184.     if (img->sub_img)
  185.         XDrawLine(img->dpy, img->win, img->gc, img->mark_x, 0,
  186.         img->mark_x, img->height);
  187.     img->mark_x = x;
  188. }
  189. XDrawLine(img->dpy, img->win, img->gc, img->mark_x, 0,
  190.     img->mark_x, img->height);
  191. XSetFunction(img->dpy, img->gc, GXcopy);
  192. return    x;
  193. }
  194.  
  195. win_exposure(expose, img, sub_win)
  196. register XExposeEvent    *expose;
  197. Image    *img;
  198. {
  199. register int    modulo = expose->x & 3, w = expose->width;
  200.     if (modulo) {
  201.         expose->x -= modulo;
  202.         w += modulo;
  203.     }
  204.     if (modulo = w & 3)    w += 4 - modulo;
  205.  
  206.     exposure_r(img, sub_win, expose->x, expose->y, w, expose->height,
  207.         img->color_dpy);
  208. }
  209.  
  210. exposure_r(img, sub_win, x, y, w, h, cdp)
  211. register Image    *img;
  212. int    (*sub_win)();
  213. {
  214. int    x0 = x,    y0 = y;
  215. #ifndef    SCROLLBAR_on_CANVAS
  216.     x0 += img->x0;    y0 += img->y0;
  217. #endif
  218.     if (cdp && img->refresh_pixmap)
  219.         XCopyArea(img->dpy, img->refresh_pixmap, img->win, img->gc,
  220.             x0, y0, w, h, x, y);
  221.     else    XPutImage(img->dpy, img->win, img->gc, img->image,
  222.             x0, y0, x, y, w, h);
  223.     if (img->sub_img){
  224.     XRectangle    r;
  225.         r.x = x;    r.y = y;
  226.         r.width = w;    r.height = h;
  227.         XSetClipRectangles(img->dpy, img->gc, 0, 0, &r, 1, Unsorted);
  228.         sub_win(img, 0, 1, img->sub_img);
  229.         XSetClipMask(img->dpy, img->gc, None);
  230.     }
  231.     else if (abs(img->tmp_offset > img->font_h<<1))    /* ??? */
  232.         img->tmp_offset = 0;
  233.     superimpose_images(img, -1, x, y, w, h);
  234.     Draw_ImageScrollBars(img);
  235. }
  236.  
  237. superimpose_add_elem(img, lw, color, shape, y, x, rows, cols)
  238. register U_IMAGE*    img;
  239. {
  240. int    i = shape<0, who = i?img->texts++:img->draws++;
  241. register superimpose_elems*    si_ep;
  242. if (!img->superimpose)    img->superimpose = zalloc(2, sizeof(caddr_t));
  243. if (who++)
  244.     img->superimpose[i] = realloc(img->superimpose[i], who*sizeof(*si_ep));
  245. else    img->superimpose[i] = nzalloc(who, sizeof(*si_ep));
  246. si_ep = (superimpose_elems*) img->superimpose[i] + who - 1;
  247. si_ep->color = color;
  248. si_ep->e_type = shape;
  249. if (i)    {
  250.     si_ep->elem.text.content = str_save((char*)lw);
  251.     si_ep->y0 = y;
  252.     si_ep->x0 = x;
  253.     si_ep->h = (si_ep->rows = rows) * (si_ep->elem.text.fh=img->font_h);
  254.     si_ep->w = (si_ep->cols = cols) * (si_ep->elem.text.fw=img->font_w);
  255. } else    {
  256.     si_ep->elem.draw.line_w = lw;
  257.     si_ep->elem.draw.join = x;
  258.     si_ep->elem.draw.style = y;
  259.     si_ep->y0 = img->sub_img_y;
  260.     si_ep->x0 = img->sub_img_x;
  261.     si_ep->h = img->sub_img_h;
  262.     si_ep->w = img->sub_img_w;
  263.     }
  264. }
  265.  
  266. /* return pos (draws), or neg (text)    */
  267. on_superimpose_elem(img, x0, y0)
  268. U_IMAGE*    img;
  269. register int    x0, y0;
  270. {
  271. if (img->superimpose)    {
  272. register superimpose_elems*    si_ep = img->superimpose[0];
  273. register int    i = img->draws;
  274.  
  275.     while (i--)    {
  276.     register int    lx, ly=lx=0, lw=si_ep[i].elem.draw.line_w;
  277.         if (si_ep[i].e_type==1 /* DrawsArc */)
  278.         lx = si_ep[i].w,    ly = si_ep[i].h;
  279.         if (si_ep[i].y0 - ly - lw < y0 && si_ep[i].x0 - lx - lw < x0 &&
  280.         si_ep[i].y0 + si_ep[i].h + lw > y0 &&
  281.         si_ep[i].x0 + si_ep[i].w + lw > x0)    return    ++i;
  282.     }
  283.     i = img->texts;
  284.     si_ep = img->superimpose[1];
  285.     while (i--)
  286.         if (si_ep[i].y0 - si_ep[i].elem.text.fh < y0 &&
  287.         si_ep[i].x0 - si_ep[i].elem.text.fw < x0 &&
  288.         si_ep[i].y0 + si_ep[i].h > y0 &&
  289.         si_ep[i].x0 + si_ep[i].w > x0)    return    ~i;
  290. }
  291. return    False;
  292. }
  293.